﻿using System;
using System.IO.Ports;
using System.Net;
using System.Threading.Tasks;

namespace Jodell.Transport.Modbus
{
    public interface IModbusClient
    {
        /// <summary>
        /// 是否连接
        /// </summary>
        bool IsConnected { get; }
        /// <summary>
        /// 写入超时时间
        /// </summary>
        int WriteTimeout { get; set; }
        /// <summary>
        /// 读取超时时间
        /// </summary>
        int ReadTimeout { get; set; }
        /// <summary>
        /// Modbus 解析器
        /// </summary>
        IModbusResolver Resolver { get; set; }
        /// <summary>
        /// 连接
        /// </summary>
        void Connect();
        /// <summary>
        /// 断开连接
        /// </summary>
        void Disconnect();
        /// <summary>
        /// 读保持寄存器 0x03
        /// </summary>
        /// <param name="unitIdentifier"></param>
        /// <param name="startingAddress"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        byte[] ReadHoldingRegisters(byte unitIdentifier, ushort startingAddress, ushort quantity);
        /// <summary>
        /// 写多个
        /// </summary>
        /// <param name="unitIdentifier"></param>
        /// <param name="startingAddress"></param>
        /// <param name="dataset"></param>
        void WriteMultipleRegisters(byte unitIdentifier, ushort startingAddress, byte[] dataset);
        /// <summary>
        /// 升级
        /// </summary>
        /// <param name="unitIdentifier"></param>
        /// <param name="recordId"></param>
        /// <param name="regLength"></param>
        /// <param name="data"></param>
        byte[] WriteFileRecord(byte unitIdentifier, byte deviceNo, UInt16 recordId, UInt16 regLength, byte[] data);
    }

    public interface IModbusRtuNetClient : IModbusClient
    {
        IPAddress IPAddress { get; set; }
        UInt16 Port { get; set; }
        Task ConnectAsync();
    }

    public interface IModbusRtuClient : IModbusClient
    {
        /// <summary>
        /// 串口名称
        /// </summary>
        string PortName { get; set; }
        /// <summary>
        /// 波特率
        /// </summary>
        int BaudRate { get; set; }
        /// <summary>
        /// 校验方式
        /// </summary>
        Parity Parity { get; set; }
        /// <summary>
        /// 停止位
        /// </summary>
        StopBits StopBits { get; set; }
        /// <summary>
        /// 通讯周期
        /// </summary>
        int Cycle { get; }
    }
}
